using System;
using System.Windows.Forms;

using FreeFlow.Client;

namespace FreeFlow.WinFormsControls
{
	/// <summary>
	/// A radio group control.
	/// </summary>
	public class RadioGroupControl : GroupBox, IFillable, IRefillable
	{
    /// <summary>
    /// Creates a new <see cref="RadioGroupControl"/> instance.
    /// </summary>
		public RadioGroupControl()
		{
    }

    #region IFillable Members

    /// <summary>
    /// Renders the radio group with static layout information using the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Render(FreeFlow.Client.Field field)
    {
      ForeColor = field.Form.FontColor;
      RadioGroupField radioGroupField = (RadioGroupField)field;
      Text = radioGroupField.Caption;
      orientation = radioGroupField.Orientation;

      for (int i = 0; i < radioGroupField.OptionCount; i++)
      {
        RadioButton button = new RadioButton();
        button.Text = radioGroupField.Option(i);
        Controls.Add(button);
      }
      PositionRadioButtons();
    }

    /// <summary>
    /// Fills the radio group with values from the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Fill(FreeFlow.Client.Field field)
    {
      if (field != null)
      {
        // disconnect event handlers to stop refill
        for (int i = 0; i < Controls.Count; i++)
          ((RadioButton)Controls[i]).CheckedChanged -= 
            new EventHandler(button_CheckedChanged);

        for (int i = 0; i < Controls.Count; i++)
        {
          if (Controls[i].Text == field.Value)
          {
            ((RadioButton)Controls[i]).Checked = true;
          }
        }

        // reconnect event handlers
        for (int i = 0; i < Controls.Count; i++)
          ((RadioButton)Controls[i]).CheckedChanged += 
            new EventHandler(button_CheckedChanged);
      }
    }

    /// <summary>
    /// Fills the field from the control.
    /// </summary>
    /// <param name="field">Field.</param>
    public void FillField(Field field)
    {
      if (field != null)
      {
        for (int i = 0; i < Controls.Count; i++)
        {
          if (((RadioButton)Controls[i]).Checked)
          {
            field.Value = Controls[i].Text;
          }
        }
      }
    }

    #endregion
    #region IRefillable Members

    /// <summary>
    /// Event triggered when a refill should occur
    /// </summary>
    public event System.EventHandler Refill;

    /// <summary>
    /// Triggers a refill.
    /// </summary>
    private void button_CheckedChanged(object sender, EventArgs e)
    {
      if ((sender as RadioButton).Checked)
        if (Refill != null)
          Refill(this, e);
    }

    #endregion

    /// <summary>
    /// Repositions the radio buttons when the control resizes.
    /// </summary>
    /// <param name="e">E.</param>
    protected override void OnResize(EventArgs e)
    {
      base.OnResize(e);
      PositionRadioButtons();
    }

    private const int topOffset = 15;
    private const int borderWidth = 5;
    private FreeFlow.Client.Orientation orientation;
    private void PositionRadioButtons()
    {
      for (int i = 0; i < Controls.Count; i++)
      {
        if (orientation == FreeFlow.Client.Orientation.Vertical)
        {
          Controls[i].Top = topOffset+((Height-topOffset-borderWidth)*i)/Controls.Count;
          Controls[i].Left = 10;
          Controls[i].Width = Width - Controls[i].Left - 3;
        }
        else
        {
          Controls[i].Left = borderWidth+(Width-2*borderWidth)*i/Controls.Count;
          Controls[i].Top = topOffset;
          Controls[i].Width = (Width - 2*borderWidth)/Controls.Count;
        }
      }
    }
  }
}
